package com.learning.railwayreservation.dao;

import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

public class TestCursorDao extends BaseDao {
	public static void main(String[] args) {

		TestCursorDao testCursorDao = new TestCursorDao();
		testCursorDao.testResultSet();
		}
    public void testResultSet() {
		try {
			DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
			Connection conn = getConnection();
			CallableStatement stmt = conn.
			prepareCall("BEGIN types.check_ticket_availability(?, ?, ?, ?, ?, ?); END;");
			
			stmt.setInt(1, 1);
			stmt.setInt(2, 1);
			stmt.setString(3, "AC");
			stmt.setInt(4, 4);
			stmt.setDate(5, Date.valueOf("2003-05-03"));
			stmt.registerOutParameter(6, OracleTypes.CURSOR); //REF CURSOR
			stmt.execute();
			ResultSet rs = (ResultSet) ((OracleCallableStatement) stmt).getObject(6);
			while (rs.next()) {
				System.out.println(rs.getString("train_name") + ":"
						+ rs.getInt("b_id") + ":" + rs.getString("row_id")
						+ ":" + rs.getInt("seat_id"));
			}
			rs.close();
			rs = null;
			stmt.close();
			stmt = null;
			conn.close();
			conn = null;
		} catch (SQLException e) {
			System.out.println(e.getLocalizedMessage());
		}
	}

}